parent.say();

// 表单验证
layui.use(['form', 'code', 'laydate','upload','layer','laytpl','element'], function() {
	var laydate = layui.laydate;
	form = layui.form;
	upload = layui.upload;
	layer = layui.layer;
	laytpl = layui.laytpl;
	element = layui.element;


	layui.code();



	function chinaIdChecker(id) {
		this.isOK = false;
		this.error = '';

		if (!id || typeof (id) != 'string' || id.length != 15 && id.length != 18
			|| !id.match(/^[0-9]{15}$/) && !id.match(/^[0-9]{17}[0-9xX]$/) || "111111111111111" == id) {
			layer.msg('输入不是15位或者18位有效身份证');
			return false;
		}

		var area = {
			11 : "北京",
			12 : "天津",
			13 : "河北",
			14 : "山西",
			15 : "内蒙古",
			21 : "辽宁",
			22 : "吉林",
			23 : "黑龙江",
			31 : "上海",
			32 : "江苏",
			33 : "浙江",
			34 : "安徽",
			35 : "福建",
			36 : "江西",
			37 : "山东",
			41 : "河南",
			42 : "湖北",
			43 : "湖南",
			44 : "广东",
			45 : "广西",
			46 : "海南",
			50 : "重庆",
			51 : "四川",
			52 : "贵州",
			53 : "云南",
			54 : "西藏",
			61 : "陕西",
			62 : "甘肃",
			63 : "青海",
			64 : "宁夏",
			65 : "新疆",
			71 : "台湾",
			81 : "香港",
			82 : "澳门",
			91 : "国外"
		};

		this.areaName = area[id.substr(0, 2)];
		if (!this.areaName) {
			layer.msg('前2位不是有效的行政区划代码');
			return false;
		}
		;

		if (id.length == 15) {
			this.year = parseInt(id.substr(6, 2));
			this.month = parseInt(id.substr(8, 2));
			this.day = parseInt(id.substr(10, 2));
		}
		else {
			this.year = parseInt(id.substr(6, 4));
			this.month = parseInt(id.substr(10, 2));
			this.day = parseInt(id.substr(12, 2));
		}

		this.error = '出生日期不正确';
		if (this.month > 12) {
			layer.msg(this.error);
			return false;
		}
		if (this.day > 31) {
			layer.msg(this.error);
			return false;
		}
		// February can't be greater than 29 (leap year calculation comes later)
		if ((this.month == 2) && (this.day > 29)) {
			layer.msg(this.error);
			return false;
		}
		// check for months with only 30 days
		if ((this.month == 4) || (this.month == 6) || (this.month == 9)
			|| (this.month == 11)) {
			if (this.day > 30) {
				layer.msg(this.error);
				return false;
			}
		}
		// if 2-digit year, use 50 as a pivot date
		if (this.year < 100) {
			this.year += 1900;
		}
		if (this.year > 9999) {
			layer.msg(this.error);
			return false;
		}
		// check for leap year if the month and day is Feb 29
		if ((this.month == 2) && (this.day == 29)) {
			var div4 = this.year % 4;
			var div100 = this.year % 100;
			var div400 = this.year % 400;
			// if not divisible by 4, then not a leap year so Feb 29 is invalid
			if (div4 != 0) {
				layer.msg(this.error);
				return false;
			}
			// at this point, year is divisible by 4. So if year is divisible by
			// 100 and not 400, then it's not a leap year so Feb 29 is invalid
			if ((div100 == 0) && (div400 != 0)) {
				layer.msg(this.error);
				return false;
			}
		}
		this.yearStr = '' + this.year;
		this.monthStr = (this.month < 10 ? '0' : '') + this.month;
		this.dayStr = (this.day < 10 ? '0' : '') + this.day;

		// date is valid
		var birthDay = new Date(this.year, this.month - 1, this.day);

		if (birthDay - new Date() >= 0 || birthDay - new Date(1850, 1, 1) <= 0) {
			layer.msg(this.error);
			return false;
		}

		this.error = '';
		var lastNum = id.length == '15' ? id.substr(14, 1) : id.substr(16, 1);
		this.sex = (lastNum == '1' || lastNum == '3' || lastNum == '5'
			|| lastNum == '7' || lastNum == '9') ? '1' : '0';
		this.sexName = this.sex == '1' ? '男' : '女';
		if (id.length == '15') {
			this.isOK = true;
			return true;
		}

		var getLastValidationLetter = function(str) {
			var strArray = new Array(17);
			// 存储身份证的前17为数字
			var Wi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2,
				1);
			// 表示第i位置上的加权因子
			var Y = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
			// 校验码值
			var S = 0;
			// 十七位数字本体码加权求和
			var jym = 0;
			// 校验码

			for ( var i = 16;
				  i >= 0;
				  i -= 1) {
				strArray[i] = Number(str.charAt(i));
			}

			for ( var j = 16;
				  j >= 0;
				  j -= 1) {
				S += strArray[j] * Wi[j];
			}

			jym = S % 11;
			return Y[jym];
		};

		if (id.substr(17, 1) != getLastValidationLetter(id.substr(0, 17))) {
			layer.msg('18位身份证编码最后一位校验码不正确');
			return false;
		}

		this.isOK = true;
		return true;
	}


	function setShowDiv(){
		AJAX.query(cgiMain.applicantCertificateInfo,
			{},
			function (res) {
				if(res.code=='0000'){


					if(res.result.verifyStatus == 4){
						$(".alert-warning").html("认证已通过，请完善您的基本信息，发布研究内容，前往推优。");
					}

					// if(res.result.verifyStatus == 4){
					// 	$(".certification_h5_show").show();
					// 	$(".certification_h5").hide();
					// }else {
					// 	$(".certification_h5_show").hide();
					// 	$(".certification_h5").show();
					// }

					$(".certification_h5").show();

					setTimeout(function () {
						setCertifiateInfoToDom(res.result);
					},100);

				}else{

					$(".certification_h5").show();
				}
			});
	}
	setTimeout(function () {
		setShowDiv();
	},500);

	function setCertifiateInfoToDom(certificateInfo){
		$("input[name=id]").val(certificateInfo.id);


		$(".showRealName").html(certificateInfo.realName);
		$(".realName").val(certificateInfo.realName);

		$(".showIdcardNum").html(certificateInfo.idcardNum);
		$(".idcardNum").val(certificateInfo.idcardNum);

		$(".idcardType").val(certificateInfo.idcardType);
		$(".showIdcardType").html(certificateInfo.idcardType==1?'身份证':certificateInfo.idcardType==2?'港澳台证':certificateInfo.idcardType==3?'护照':certificateInfo.idcardType==4?'军官证':'');



		$(".showIdFrontImage").prop("src",certificateInfo.idFrontImage);
		$(".editFrontImg").prop("src",certificateInfo.idFrontImage);
		$(".idFrontImage").val(certificateInfo.idFrontImage);

		$(".showIdBackImage").prop("src",certificateInfo.idBackImage);
		$(".editBackImg").prop("src",certificateInfo.idBackImage);
		$(".idBackImage").val(certificateInfo.idBackImage);

		if(certificateInfo.verifyStatus == 2){
			$(".alert-warning").html("正在审核中，请耐心等候...您只有认证通过，才能推优项目。");
		}
		if(certificateInfo.verifyStatus == 3){
			$(".alert-warning").html("您的认证失败，原因是：" + certificateInfo.message);
		}

		if(certificateInfo.verifyStatus == 4){
			$(".saveBtn").html("修改");
		}

		form.render();
		form.render('select');

	}




	$(".saveBtn").on("click", function() {
		// 判断必填项--提示--统校验
		var params = {};

		// 集合所有参数
		$("#applicantCertificateForm .save_val").each(function() {
			var name = $(this).attr("name");
			var val = $(this).val();
			params[name] = val;
		});

		if(!params.realName || !params.idcardNum){
			layer.msg("请填写完整身份证信息");
			return false;
		}

		if(params.idcardType == 1 && !chinaIdChecker(params.idcardNum)){
			return false;
		}

		if(!params.idFrontImage || !params.idBackImage){
			layer.msg("请上传完整证件扫描件");
			return false;
		}

		params.roleId=1;

		var reVerify = false;

		if(params.id && params.id > 0){
			reVerify = true;
			layer.open({
				content: '重新提交会取消已有的认证状态，重新认证！确定提交吗?'
				, btn: ['确定', '取消']
				, yes: function (index, layero) {
					//按钮【按钮一】的回调
					handleSubmit(params);
				}
			});
		}

		if(reVerify){
			return false;
		}
		handleSubmit(params);
		return false;
	});

	function handleSubmit(params) {
		var url =cgiMain.applicantCertificateAdd;

		if(params.id && params.id > 0){
			url = cgiMain.applicantCertificateEdit;
		}


		AJAX.post(url,
			params,
			function (res) {
				if(res.code=='0000'){
					layer.msg("提交成功");
					setTimeout(function () {
						window.location.reload();
					},1000);

				}else{
					layer.msg(res.msg);
				}


			});
		setShowDiv();
		console.log(params);
	}


	//执行实例
	var uploadInst = upload.render({
		elem: '.upFile_btn_front', //绑定元素
		url: cgiMain.uploadFile, //上传接口
		done: function(res) {
			//上传完毕回调
			$(".editFrontImg").prop("src",res.qiNiuFilePath);
			$("input[name='idFrontImage']").val(res.qiNiuFilePath);
			// $(".photoShow").removeClass("JR_hide");
		},
		error: function() {
			//请求异常回调
		}
	});

	//执行实例
	var uploadInst = upload.render({
		elem: '.upFile_btn_back', //绑定元素
		url: cgiMain.uploadFile, //上传接口
		done: function(res) {
			//上传完毕回调
			$(".editBackImg").prop("src",res.qiNiuFilePath);
			$("input[name='idBackImage']").val(res.qiNiuFilePath);
			// $(".photoShow").removeClass("JR_hide");
		},
		error: function() {
			//请求异常回调
		}
	});





});


// 页面相关方法
var page = {
	// 初始化
	init: function() {
		// 提交验证
		$(".saveBtn").on("click", function() {
			// 判断必填项--提示、
			var params = {
				"name": $(".name").val(),
				"type": $(".type").val(),
				"code": $(".code").val(),
				"frontPhoto": $(".front_photo").val(),
				"backPhoto": $(".back_photo").val()
			};

			console.log(params);
		});
	},

	// 立即验证
	saveInfo: function(params) {
		var url = cgiMain.getVendorList;
		AJAX.query(url, params, function(res) {
			console.log(res)
		});
	},

	// 获取验证信息
	getInfo: function() {
		var url = cgiMain.getVendorList;
		AJAX.query(url, params, function(res) {
			console.log(res)
		});
	}
};

page.init(); // 初始化页面内容和事件处理
